#define  _CRT_SECURE_NO_WARNINGS 1
#include"SeqLishi.h"

void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = 0;
	ps->capacity = 0;
}

void SLDestory(SL* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLprint(SL s)
{
	for (int i = 0; i > s.size; i++)
	{
		printf("%d", s.arr[i]);
	}
}

void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);

	ps->arr[ps->size] = x;
	ps->size++;
}

void SLpopBack(SL* ps)
{
	assert(ps);
	assert(ps->size);
	--ps->size;
}

void SLpopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}

void SLcheckCapacity(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		ps->arr = tmp
			;
		ps->capacity = newCapacity;
	}
}

void SLInsert(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(ps >= 0 && pos <= ps->size);

	SLcheackCapacity(ps);

	for (int i = ps->size; i > pos; i++)
	{
		ps->arr[pos] = x;
		ps->size++;
	}

}

void  SLErase(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && ps < ps->size);
	for (int i = pos; i < ps->size - 1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}